package com.upc.feedish;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.upc.feedish.utils.DataBaseController;

public class RegisterActivity extends AppCompatActivity {
    ImageView imageView;
    Handler handler;
    DataBaseController dataBaseController;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        dataBaseController = new DataBaseController();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap=dataBaseController.getCap();
                Message msg = Message.obtain();
                msg.obj = bitmap;//传递的数据
                handler.sendMessage(msg);
            }
        }).start();

         handler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {
                Bitmap bitmap=(Bitmap) msg.obj;
                imageView.setImageBitmap(bitmap);
                return true;
            }
        });

        Toast toastInternetError = Toast.makeText(this, "Internet Error!",Toast.LENGTH_SHORT);
        Toast toastSuccess = Toast.makeText(this, "Success!",Toast.LENGTH_SHORT);
        Toast toastFailure = Toast.makeText(this, "Duplicate username!",Toast.LENGTH_SHORT);
        Toast toastNull = Toast.makeText(RegisterActivity.this, "请填写用户名或密码和验证码",Toast.LENGTH_SHORT);
        Toast toastCap = Toast.makeText(this, "验证码错误!",Toast.LENGTH_SHORT);

        Button registerButton = findViewById(R.id.button_register);
        EditText usernameText= findViewById(R.id.registerUsername);
        EditText passwordText = findViewById(R.id.registerPassword);
        EditText capText = findViewById(R.id.captcha);
        imageView=findViewById(R.id.chap);

        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(usernameText.getText().toString().equals("")
                        ||passwordText.getText().toString().equals("")
                        ||capText.getText().toString().equals("")){
                    toastNull.show();
                }
                else {
                    PopupWindow popupWindow = new PopupWindow();
                    popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                    popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                    popupWindow.setFocusable(true);
                    View view = LayoutInflater.from(RegisterActivity.this).inflate(R.layout.popup,null);
                    popupWindow.setContentView(view);
                    popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.CENTER,0,0);
                    Handler mh=new Handler() {
                        @Override
                        public void handleMessage(@NonNull Message msg) {
                            super.handleMessage(msg);
                            popupWindow.dismiss();
                        }
                    };
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            int flg=dataBaseController.register(usernameText.getText().toString()
                                    ,passwordText.getText().toString(),capText.getText().toString());
                            if(flg==1){
                                toastSuccess.show();
                                Intent intent=new Intent();
                                intent.putExtra("username",usernameText.getText().toString());
                                intent.putExtra("password",passwordText.getText().toString());
                                setResult(1,intent);
                                mh.sendMessage(new Message());
                                finish();
                            }else if(flg==-2){
                                toastInternetError.show();
                                mh.sendMessage(new Message());

                            }else if(flg==-1){
                                toastCap.show();
                                mh.sendMessage(new Message());
                            } else{
                                toastFailure.show();
                                mh.sendMessage(new Message());
                            }
                        }
                    }).start();

                }
            }
        });
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Bitmap bitmap=dataBaseController.getCap();
                        Message msg = Message.obtain();
                        msg.obj = bitmap;//传递的数据
                        handler.sendMessage(msg);
                    }
                }).start();
            }
        });

    }



}